package com.shj.em.biz.warehouse.controller;

import com.shj.em.base.controller.BaseController;
import com.shj.em.base.support.ResMsg;
import com.shj.em.base.vo.PageVO;
import com.shj.em.biz.Constants;
import com.shj.em.biz.company.entity.Company;
import com.shj.em.biz.company.entity.Contact;
import com.shj.em.biz.sell.entity.Order;
import com.shj.em.biz.prod.entity.OrderOutdate;
import com.shj.em.biz.prod.service.OrderOutdateService;
import com.shj.em.biz.sys.entity.Cd;
import com.shj.em.biz.sys.service.CdService;
import com.shj.em.biz.warehouse.entity.Shipment;
import com.shj.em.biz.warehouse.service.ShipmentService;
import com.shj.em.common.annotation.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单信息 前端控制器
 * </p>
 *
 * @author Shen Huang Jian
 * @since 2018-07-09
 */
@Controller
@Slf4j
@RequestMapping("/shipment")
public class ShipmentController extends BaseController<Order> {
    private String prefix = "warehouse";

    @Autowired
    private OrderOutdateService outdateService;

    @Autowired
    private ShipmentService shipmentService;

    @GetMapping("/gotoSearchOrder")
    public String gotoSearchOrder(Model model){
        addRoleInfo(model);
        return prefix + "/searchOrder";
    }

    /**
     * 是否已出货的查询
     * @param model
     * @return
     */
    @GetMapping("/gotoSearch")
    public String gotoSearch(Model model){
        model.addAttribute("outDate", new Date());
        model.addAttribute("outStatus", "N");
        return prefix + "/shipment_search";
    }

    @GetMapping("/gotoChange")
    public String gotoChange(){
        return "prod/outdate/search";
    }

    @GetMapping("/gotoAddAdjust/{orderId}")
    public String gotoAddAdjust(@PathVariable("orderId") Long orderId, Model model){
        Order order = orderService.selectById(orderId);
        model.addAttribute("order", order);

        OrderOutdate orderOutDt = new OrderOutdate();
        orderOutDt.setOrderId(order.getId());
        orderOutDt.setOutDt(order.getPlanOutDt() == null ? new Date(): order.getPlanOutDt());
        model.addAttribute("orderOutDt", orderOutDt);

        List<Cd> reasons = cdService.queryCdsByType(CdService.CD_ADJUST_REASON);
        model.addAttribute("reasons", reasons);
        return "prod/outdate/adjust";
    }

    @Log("调整出货日期")
    @PostMapping("/changeOutDate")
    @ResponseBody
    public ResMsg changeOutDate(OrderOutdate orderOutdate){
        outdateService.changePlanOutDt(orderOutdate);
        return ResMsg.ok();
    }

    /**
     * 去维护出货页面
     * @param orderId
     * @param model
     * @return
     */
    @RequestMapping("/gotoMaintainShipment/{orderId}")
    public String gotoEditOutDate(@PathVariable("orderId") Long orderId, Model model){
        Order order = orderService.selectById(orderId);
        model.addAttribute("order", order);

//        if(order.getOutDt() == null){
//            order.setOutDt(new Date());
//        }

        String custName = companyService.selectById(order.getCustId()).getName();
        order.setCustName(custName);

//        String contactName = contactService.selectById(order.getContactId()).getName();
//        order.setContactName(contactName);
        return "warehouse/maintainShipment";
    }

    /**
     * 加载出货记录表
     * @param orderId
     * @return
     */
    @GetMapping("/listShipment")
    @ResponseBody
    public PageVO<Shipment> listShipment(@RequestParam("orderId") Long orderId){
        List<Shipment> list = shipmentService.queryShipmentByOrderId(orderId);
        PageVO<Shipment> pageVO = new PageVO<>();
        pageVO.setTotal(list.size());
        pageVO.setRows(list);
        return pageVO;
    }

    @RequestMapping("/gotoAdd/{orderId}")
    public String gotoAdd(@PathVariable("orderId") Long orderId, Model model){
        Shipment shipment = new Shipment();
        shipment.setShipmentDt(new Date());
        shipment.setOrderId(orderId);
        addOtherInfo(shipment, model, false);
        model.addAttribute("isEdit", false);
        return prefix + "/addShipment";
    }

    private void addOtherInfo(Shipment shipment, Model model, boolean isEdit){
        model.addAttribute("shipment", shipment);
        Order order = orderService.selectById(shipment.getOrderId());
        List<Contact> contactList = contactService.findContactsByCompanyId(order.getCustId());
        model.addAttribute("contactList", contactList);
        if(isEdit){
            for(Contact contact : contactList){
                if(contact.getName().equals(shipment.getContactName())){
                    shipment.setContactId(contact.getId());
                    break;
                }
            }
        }else{
            Company company = companyService.selectById(order.getCustId());
            shipment.setAddress(company.getAddress());
        }

    }

    @RequestMapping("/gotoEdit/{shipmentId}")
    public String gotoEdit(@PathVariable("shipmentId") Long shipmentId, Model model){
        Shipment shipment = shipmentService.selectById(shipmentId);
        addOtherInfo(shipment, model, true);
        model.addAttribute("isEdit", true);
        return prefix + "/addShipment";
    }

    @Log("创建出货记录")
    @ResponseBody
    @PostMapping("/add")
    public ResMsg add(Shipment shipment){
        shipmentService.insertShipment(shipment);
        return ResMsg.ok();
    }

    @Log("更新出货记录")
    @ResponseBody
    @PostMapping("/edit")
    public ResMsg edit(Shipment shipment){
        shipmentService.updateById(shipment);
        return ResMsg.ok();
    }

    /*@Log("设置出货日期")
    @PostMapping("/editOutDate")
    @ResponseBody
    public ResMsg editOutDate(Order order){
        orderService.editOutDate(order);
        return ResMsg.ok();
    }*/

    /*@Log("批量删除出货记录")
    @PostMapping( "/batchRemove")
    @ResponseBody
    public ResMsg batchRemove(@RequestParam("ids[]") Long[] ids){
        shipmentService.deleteBatchIds(Arrays.asList(ids));
        return ResMsg.ok();
    }*/

    @RequestMapping("/allDelivered/{orderId}")
    public String setAllDelivered(@PathVariable("orderId") Long orderId, Model model){
        Order order = orderService.selectById(orderId);
        order.setShipmentStatus(Constants.SHIPMENT_STATUS_FINISH);
        orderService.updateById(order);
        model.addAttribute("order", order);

        String custName = companyService.selectById(order.getCustId()).getName();
        order.setCustName(custName);

        return prefix + "/viewShipment";
    }

    @RequestMapping("/gotoViewShipment/{orderId}")
    public String gotoViewShipment(@PathVariable("orderId") Long orderId, Model model){
        Order order = orderService.selectById(orderId);
        model.addAttribute("order", order);

        String custName = companyService.selectById(order.getCustId()).getName();
        order.setCustName(custName);

        return prefix + "/viewShipment";
    }


}
